﻿using chihuopig.IService;
using System;
using System.Collections.Generic;
using System.Text;
using chihuopig.Model;
using System.Threading.Tasks;
using SqlSugar;

namespace chihuopig.Service
{
    public class WriterService :DBService, IWriterService
    {
        public Task<int> delAll(long[] Id)
        {
            return Task.Factory.StartNew<int>(s => {
                string ls = string.Join(",", (long[])s);
                string sql = $"update Writer set IsDelete='1' where id in ({ls})";
                return
                Sqlclient.Ado
               .ExecuteCommand(sql);
            }, Id);
        }

        public Task<int> Deleteable(long Id)
        {
            return Sqlclient
                   .Updateable<Writer>()
                   .UpdateColumns(it => new Writer() { IsDelete = true })
                   .Where(c => c.Id == Id)
                    .ExecuteCommandAsync();
        }

        public Task<Writer> EditAsync(long Id)
        {
            return Sqlclient.Queryable<Writer>().Where(s => s.Id == Id).FirstAsync();
        }

        public Task<List<Writer>> GetPage(int pageIndex, int PageSize)
        {
            return Sqlclient.Queryable<Writer>().Where(c => c.IsDelete == false).OrderBy(s => s.Addtime, SqlSugar.OrderByType.Desc).ToPageListAsync(pageIndex, PageSize);
        }

        public Task<double> GetPageTotal(int PageSize)
        {
            return Task.Factory.StartNew<double>(s =>
            {
                double size = Convert.ToDouble(s);
                double bnum = Sqlclient
                .Queryable<Writer>()
                .Where(w => w.IsDelete == false )
                .OrderBy(w => w.Addtime, SqlSugar.OrderByType.Desc).Count() / size;
                //Math.Ceiling(bnum);
                return Math.Ceiling(bnum);
            }, PageSize);
        }

        public Task<long> Insertable(Writer Model)
        {
            return Sqlclient.Insertable(Model).ExecuteReturnBigIdentityAsync();
        }

        public Task<int> Updateable(Writer Model)
        {
            return Sqlclient.Updateable(Model).ExecuteCommandAsync();
        }
    }
}
